package org.aristotle.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.user.model.dto.RoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
// 本模块
import org.aristotle.user.model.entity.UserRole;
import org.aristotle.user.model.vo.UserRoleVO;
import org.aristotle.user.mapper.UserRoleMapper;
import org.aristotle.user.service.IUserRoleService;

// 一对多(本类是多)
// 多对一(本类是多)
import org.aristotle.user.model.entity.Role;
import org.aristotle.user.service.IRoleService;
import org.aristotle.user.model.vo.RoleVO;
import org.aristotle.user.model.entity.User;
import org.aristotle.user.service.IUserService;
import org.aristotle.user.model.vo.UserVO;

import java.util.List;
import java.util.stream.Collectors;
// 多对多关系

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    // 多对一(本类是多)
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserService userService;

    // 一对多(本类是一)

    // 多对多关系中间表

    @Override
    public Page<UserRoleVO> queryPageList(
        Long id,
        Long roleId,
        Long userId,
        Integer current,
        Integer size
    ) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper();

        if (id != null) {
            queryWrapper.eq("id",id);
        }


            if (roleId != null) {
                queryWrapper.eq("role_id",roleId);
            }
            if (userId != null) {
                queryWrapper.eq("user_id",userId);
            }

        Page<UserRole> page = this.page(new Page<UserRole>(current,size), queryWrapper);

        // 转为VO
        Page<UserRoleVO> result = AristotleBeanUtils.transitionBean(page, Page<UserRoleVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),UserRoleVO::new));

        result.getRecords().forEach(userRoleVO -> {
            // 多对一(本类是多)
            RoleVO roleVO = this.roleService.getById(userRoleVO.getRoleId());
            userRoleVO.setRole(roleVO);
            UserVO userVO = this.userService.getById(userRoleVO.getUserId());
            userRoleVO.setUser(userVO);
        });
        return result;
    }

    @Override
    public UserRoleVO getById(Long id) {
        UserRole userRole = this.baseMapper.selectById(id);
        UserRoleVO userRoleVO = AristotleBeanUtils.transitionBean(userRole, UserRoleVO::new);
        RoleVO roleVO = this.roleService.getById(userRoleVO.getRoleId());
        userRoleVO.setRole(roleVO);
        UserVO userVO = this.userService.getById(userRoleVO.getUserId());
        userRoleVO.setUser(userVO);
        return userRoleVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        /*删除中间表*/
        /*一对多(本类是一)*/

        return super.removeById(id);
    }

    // 根据外键删除
    public Boolean removeById(
        Long roleId
        ,Long userId
    ) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper();
        if (
            roleId == null
            && userId == null
        ) {
            return false;
        }
        if (roleId != null) {
            queryWrapper.eq("role_id",roleId);
        }
        if (userId != null) {
            queryWrapper.eq("user_id",userId);
        }
        return this.remove(queryWrapper);
    }

    @Override
    public List<RoleDTO> getRoleDTOByUserId(Long id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<UserRole> list = this.list(queryWrapper);

        return list.stream()
                .map(userRole -> this.roleService.getDTOById(userRole.getRoleId()))
                .collect(Collectors.toList());
    }
}
